﻿using System;
using System.Xml;
using System.Collections.Generic;

namespace HorrorsOfArkham.Game_Application_Layer
{
    public class XMLResourceParser
    {
        #region Members

        public static readonly XMLResourceParser Instance = new XMLResourceParser();

        #endregion

        #region Initialization

        protected XMLResourceParser()
        { 
        }

        #endregion

        #region Parsing

        public bool ParseStringXML(XmlDocument document)
        {
            XmlNodeList nodeList = document.SelectNodes("/strings/string");

            bool parsingSuccesful = false;

            if (nodeList != null)
            {
                Dictionary<string, string> strings = new Dictionary<string, string>();

                foreach (XmlNode node in nodeList)
                {
                    string value = node.Attributes["value"].Value;
                    string id = node.Attributes["id"].Value;

                    if (value.Length > 0 && id.Length > 0)
                    {
                        strings.Add(id, value);
                    }
                    else
                    {
                        Console.WriteLine("Found missing value. Value: " + value + " ID: " + id);
                    }
                }

                parsingSuccesful = XMLResourceStorageHandler.Instance.StoreStringData(strings);

                if (parsingSuccesful)
                {
                    return true;
                }
                else 
                {
                    Console.WriteLine("Saving parsed file failed.");
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Problem parsing string file.");
                return false;
            }
        }

        #endregion
    }
}
